
local start, duration = GetSpellCooldown("Shadow Word: Pain");
local gcd = start + duration;
local function GetCooldown(id)
  local start, duration = GetSpellCooldown(id);
  local cd = start + duration - gcd;
  if cd < 0 then return 0 end
  return cd
end

if (not (UnitExists("target") and UnitCanAttack("player", "target"))) or (UnitIsDeadOrGhost("target")) then
  return
end

local _, _, _, duration = IconAura("HARMFUL", "target", "Shadow Word: Pain", "player");
if (not duration) then
  return IconSpell("Shadow Word: Pain", true);
end

if not UnitBuff("player", "Empowered Shadow") then
	if UnitBuff("player", "Shadow Orb") then
		local cdMB = GetCooldown("Mind Blast");
		if (cdMB<=1) then  
		  return IconSpell("Mind Blast", true);
		end
	end
end

local _, _, _, duration = IconAura("HARMFUL", "target", "Vampiric Touch", "player");
if (not duration) then
  return IconSpell("Vampiric Touch", true);
end

local _, _, _, duration = IconAura("HARMFUL", "target", "Devouring Plague", "player");
if (not duration) then
  return IconSpell("Devouring Plague", true);
end

local cdSf = GetCooldown("Shadowfiend");
if (cdSf<=1) then  
  return IconSpell("Shadowfiend", true);
end


local VTminDuration = 6;
local DPminDuration = 4;

local _, _, start, VTduration = IconAura("HARMFUL", "target", "Vampiric Touch", "player");
if (start + VTduration - GetTime() < VTminDuration) then
    return IconSpell("Vampiric Touch", true);
end

local _, _, start, DPduration = IconAura("HARMFUL", "target", "Devouring Plague", "player");
if (start + DPduration - GetTime() < DPminDuration) then
    return IconSpell("Devouring Plague", true);
end

local c = floor(UnitHealth("target") / UnitHealthMax("target") * 100)
if (c <= 25 and GetCooldown("Shadow Word: Death")<= 1) then
    return IconSpell("Shadow Word: Death", true)
end

if UnitBuff("player", "Shadow Orb") then
	local cdMB = GetCooldown("Mind Blast");
	if (cdMB<=1) then  
	  return IconSpell("Mind Blast", true);
	end
end

return IconSpell("Mind Flay", true);